<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>菜鸟教程(runoob.com)</title>
	</head>
	<body>

		<!--对象只是一种特殊的数据。对象拥有属性和方法。-->
		<!--创建了对象的一个新实例，并向其添加了四个属性-->
		<script>
			var person=new Object();
			person.firstname="John";
			person.lastname="Doe";
			person.age=50;
			person.eyecolor="blue"; 
			document.write(person.firstname + " is " + person.age + " years old.");
		</script>

		<!--替代语法（使用对象 literals）：-->
		<script>
			person={firstname:"John", lastname:"Doe", age:50, eyecolor:"blue"}
			document.write(person.firstname + " is " + person.age + " years old.");
		</script>

		<!--使用对象构造器-->
		<script>
			function person(firstname,lastname,age,eyecolor){
				this.firstname=firstname;
				this.lastname=lastname;
				this.age=age;
				this.eyecolor=eyecolor;
			}
			myFather=new person("John","Doe",50,"blue");
			document.write(myFather.firstname + " is " + myFather.age + " years old.");
		</script>

		<!--把方法添加到 JavaScript 对象-->
		<script>
			function person(firstname, lastname, age, eyecolor){
				this.firstname=firstname;
				this.lastname=lastname;
				this.age=age;
				this.eyecolor=eyecolor;
				this.changeName=changeName;
				function changeName(name){
					this.lastname=name;
				}
			}
			myMother=new person("Sally","Rally",48,"green");
			myMother.changeName("Doe");
			document.write(myMother.lastname);
		</script>

		<!--for...in 循环遍历对象的属性-->
		<p>点击下面的按钮，循环遍历对象 "person" 的属性。</p>
		<button onclick="myFunction()">点击这里</button>
		<p id="demo"></p>
		<script>
			function myFunction(){
				var x;
				var txt="";
				var person={fname:"Bill", lname:"Gates", age:56};
				for (x in person){
					txt=txt + person[x];
				}
				document.getElementById("demo").innerHTML=txt;
			}
		</script>

	</body>
</html>
